﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using webnew.Common;
namespace webnew.Systems
{
    public partial class S_staffList : ListBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            _EditSrc = "S_staffEdit.aspx";            //编辑页路径
            _ListTbName = "Sys_Staff";             //表名
            _Grdview = grdView;                      //主列表控件
            _DelType = DeleteType.FalseDel;          //逻辑删除  
            btnExcelVisble = true;
           // btnExcelVisble = false; //设置导出不可见  
            #region 传递参数，把控件值赋值给参数
            _SrcPrams =  "&CompId=" + UcCompany1.SelectedValue + "&DeptId=" + ddlOADept.SelectedValue + "&TrueName=" + txtTrueName.Text;
            _SrcPrams += "&TitleName=" + txtTitleName.Text + "&Status=" + dllStatus.SelectedValue + "&RecordCount=" + AspNetPager1.RecordCount + "&CurrentPageIndex=" + AspNetPager1.CurrentPageIndex;
            #endregion       
            if (!IsPostBack)
            {
                txtOADept.Style.Add("visibility", "hidden");  //隐藏选中的OA部门ID
                txtHRDept.Style.Add("visibility", "hidden");  //隐藏选中的HR部门ID
                SetTitle(true, "后台管理 >> 员工管理");
                //绑定OA单位列表,初始化部门列表
                UcCompany1.show(true, "ALL");
                ////绑定HR单位列表,初始化部门列表
                //UcCompany2.show(true, "HR");
                //初始化OA部门和HR部门
                ddlOADept.Items.Insert(0, new ListItem("--请选择--", "0"));
               // ddlHRDept.Items.Insert(0, new ListItem("--请选择--", "0"));  
                #region 接收参数，并赋值给检索控件
                if (Request.QueryString["CompId"] != null) UcCompany1.SelectedValue = Request.QueryString["CompId"].ToString();
                if (Request.QueryString["DeptId"] != null) ddlOADept.SelectedValue = Request.QueryString["DeptId"].ToString();
                if (Request.QueryString["TrueName"] != null) txtTrueName.Text = Request.QueryString["TrueName"].ToString();
                if (Request.QueryString["TitleName"] != null) txtTitleName.Text = Request.QueryString["TitleName"].ToString();
                if (Request.QueryString["Status"] != null) dllStatus.SelectedValue = Request.QueryString["Status"].ToString();
                //记录分页页数 需传递总条数和当前页数 
                if (Request.QueryString["RecordCount"] != null) AspNetPager1.RecordCount = Convert.ToInt32(Request.QueryString["RecordCount"]);
                if (Request.QueryString["CurrentPageIndex"] != null) AspNetPager1.CurrentPageIndex = Convert.ToInt32(Request.QueryString["CurrentPageIndex"]);
                #endregion                   
                AspNetPager1_PageChanged(null, null);
            }
        }

        /// <summary>
        /// 通过单位ID获取部门列表
        /// </summary>
        [WebMethod]
        public static List<Model.Sys_DeptInfo> GetDeptList(string CompID)
        {
            return new BLL.Sys_Dept().GetDeptListByComp(CompID);
        }

        /// <summary>
        /// 检索按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            AspNetPager1_PageChanged(null, null);             //刷新页面
        }

        /// <summary>
        /// 重写删除事件,使删除后刷新页面;
        /// </summary>
        protected override void _Del()
        {
            //----------系统日志model

            Model.Sys_LogInfo modelLog = new webnew.Model.Sys_LogInfo();
            modelLog.UserID = UserInfor.Id;                  //用户ID
            modelLog.IP = new BLL.Sys_Log().GetUserIP(System.Web.HttpContext.Current.Request);  //IP
            modelLog.Module = new BLL.Sys_Navigation().GetModuleName(this.GetType().BaseType.Name);   //模块名
            modelLog.CreateDate = DateTime.Now;           //创建时间

            string error = "";                   //获取错误信息

            try
            {
                base._Del();
            }
            catch (Exception e)
            {
                error = e.ToString();
            }
            finally
            {
                //添加一条系统日志
                new BLL.Sys_Log().AddSysLog(error, "删除", modelLog, "员工档案", "员工档案，员工名称为" + GetNames("truename", _ListTbName));
            }
            AspNetPager1_PageChanged(null, null);
        }


        /// <summary>
        /// 分页控件事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>    
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            //分页的查询表名
            System.Text.StringBuilder strtable = new System.Text.StringBuilder();
            //要查询的字段
            System.Text.StringBuilder strFeild = new System.Text.StringBuilder();
            strFeild.Append(" b.compname as OACompName,(select deptname from Sys_Dept where id = a.OADeptId ) as OADeptName ,a.*, ");
            strFeild.Append(" (SELECT s.[TrueName] FROM [ccweb].[dbo].[Sys_Staff] s where Convert(varchar(50),s.Id)=a.LeaderId and s.isdel=0) as LeaderName ");
            strtable.Append(" Sys_Staff a join Sys_Company b on a.OACompId=b.Id  ");
            
            //strtable.Append(" join Sys_Company d on a.HRCompId=d.Id join Sys_Dept e on a.HRDeptId=e.Id  ");
            //分页的查询的条件  
            System.Text.StringBuilder _conditon = new System.Text.StringBuilder();             
            //用户姓名,职称检索条件
            _conditon.Append(" a.truename like '%" + txtTrueName.Text.Trim().Replace("'", "") + "%'");
            _conditon.Append(" and a.titleName like '%" + txtTitleName.Text.Trim().Replace("'", "") + "%'");
            //OA公司,部门检索条件
            if (!(UcCompany1.SelectedValue == Guid.Empty.ToString())) _conditon.Append(" and a.OACompId ='" + UcCompany1.SelectedValue + "'");
            if (!(txtOADept.Text == "0")) _conditon.Append(" and a.OADeptId ='" + txtOADept.Text + "'");
            ////HR公司,部门检索条件
            //if (!(UcCompany2.SelectedValue == Guid.Empty.ToString())) _conditon.Append(" and a.HRCompId ='" + UcCompany2.SelectedValue + "'");
            //if (!(txtHRDept.Text == "0")) _conditon.Append(" and a.HRDeptId ='" + txtHRDept.Text + "'");
            //状态检索条件
            if (!(dllStatus.SelectedValue == "全部"))
            {
                _conditon.Append(" and a.isdel ='" + dllStatus.SelectedValue + "'");
            }
            else _conditon.Append(" and a.isdel <> 1");


            AspNetPager1.RecordCount = new BLL.Sys_Staff().GetCount(strtable.ToString(), _conditon.ToString());             //根据条件获取相关的行数
            webnew.Common.CommonHelper.DataGrdBind(grdView, new BLL.Sys_Staff().GetPageList(strFeild.ToString(), _conditon.ToString(), grdView.PageSize, AspNetPager1.CurrentPageIndex, strtable.ToString(), "b.compname,(select deptname from Sys_Dept where id = a.OADeptId ),a.TrueName,a.id", true));
        }


        /// <summary>
        /// 重写导出事件;
        /// </summary>
        protected override void _Excel()
        {
            //分页的查询表名
            System.Text.StringBuilder strtable = new System.Text.StringBuilder();
            //要查询的字段
            System.Text.StringBuilder strFeild = new System.Text.StringBuilder();
            strFeild.Append(" b.compname as OACompName,(select deptname from Sys_Dept where id = a.OADeptId ) as OADeptName ,a.*, ");
            strFeild.Append(" (SELECT s.[TrueName] FROM [ccweb].[dbo].[Sys_Staff] s where Convert(varchar(50),s.Id)=a.LeaderId and s.isdel=0) as LeaderName ");
            strtable.Append(" Sys_Staff a join Sys_Company b on a.OACompId=b.Id  ");

            //strtable.Append(" join Sys_Company d on a.HRCompId=d.Id join Sys_Dept e on a.HRDeptId=e.Id  ");
            //分页的查询的条件  
            System.Text.StringBuilder _conditon = new System.Text.StringBuilder();
            //用户姓名,职称检索条件
            _conditon.Append(" a.truename like '%" + txtTrueName.Text.Trim().Replace("'", "") + "%'");
            _conditon.Append(" and a.titleName like '%" + txtTitleName.Text.Trim().Replace("'", "") + "%'");
            //OA公司,部门检索条件
            if (!(UcCompany1.SelectedValue == Guid.Empty.ToString())) _conditon.Append(" and a.OACompId ='" + UcCompany1.SelectedValue + "'");
            if (!(txtOADept.Text == "0")) _conditon.Append(" and a.OADeptId ='" + txtOADept.Text + "'");
            ////HR公司,部门检索条件
            //if (!(UcCompany2.SelectedValue == Guid.Empty.ToString())) _conditon.Append(" and a.HRCompId ='" + UcCompany2.SelectedValue + "'");
            //if (!(txtHRDept.Text == "0")) _conditon.Append(" and a.HRDeptId ='" + txtHRDept.Text + "'");
            //状态检索条件
            if (!(dllStatus.SelectedValue == "全部"))
            {
                _conditon.Append(" and a.isdel ='" + dllStatus.SelectedValue + "'");
            }
            else _conditon.Append(" and a.isdel <> 1");
            //ToExcelconditon.Append(" isdel=0 and titleName like '%" + txtTitleName.Text.Trim().Replace("'", "") + "%'");
            _conditon.Append(" order by b.compname,(select deptname from Sys_Dept where id = a.OADeptId ),a.TrueName,a.id  ");
            string strHeaderText = "员工管理"; //导出的Excel标题
            string filename = "员工管理";      //Excel文件名 不包括后缀名
            webnew.Common.CommonHelper.DataGrdBind(grdExcel, new BLL.Sys_Staff().GetExcelList(strFeild.ToString(), strtable.ToString(), _conditon.ToString()));
            ToExcel.GridViewToExcel(grdExcel, strHeaderText, filename);
        }

        protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            
            if (e.Row.RowType == DataControlRowType.DataRow)
            {               
                //鼠标经过变色
                e.Row.Attributes.Add("onmouseover", "GridViewMouseover(this)");
                e.Row.Attributes.Add("onmouseout", "GridViewMouseout(this)");
                e.Row.Attributes.Add("onclick", "GridViewonclick(this)");
                webnew.Common.CommonHelper.grdCellStyle(e.Row.Cells[6], 20);
                //备注提示
                e.Row.Cells[8].ToolTip = e.Row.Cells[8].Text == "&nbsp;" ? "" : e.Row.Cells[8].Text;
                //转换状态
                e.Row.Cells[9].Text=e.Row.Cells[9].Text == "0" ? "启用" :"停用";
                webnew.Common.CommonHelper.grdCellStyle(e.Row.Cells[8],12);
            }
        }

        protected void grdExcel_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //转换状态
            e.Row.Cells[8].Text = e.Row.Cells[8].Text == "0" ? "启用" : "停用";
        }

       
    }
}
